/*
 *      Copyright 2012-2013 Battams, Derek
 *       
 *       Licensed under the Apache License, Version 2.0 (the "License");
 *       you may not use this file except in compliance with the License.
 *       You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *       Unless required by applicable law or agreed to in writing, software
 *       distributed under the License is distributed on an "AS IS" BASIS,
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *       See the License for the specific language governing permissions and
 *       limitations under the License.
 */
package org.schedulesdirect.test.fvt;

import java.io.File;

import org.junit.Test;
import static org.junit.Assert.*;
import org.schedulesdirect.api.Headend;
import org.schedulesdirect.api.NetworkEpgClient;
import org.schedulesdirect.test.TestUtils;

public class Grabber {

	@Test
	public void runGrabber() throws Exception {
		final String clntType = "valid";
		String[] creds = TestUtils.getCredsFor(clntType);
		resetLineups(clntType);
		File root = new File(new File(System.getProperty("user.home")), "sd4jtest/sd4j.epg");
		String[] args = new String[] {"-pc", "2500", "-sc", "25", "-c", "grab", "-u", creds[0], "-p", creds[1], "-o", root.getAbsolutePath()};
		if(TestUtils.getTestUrl() != null) {
			String[] allArgs = new String[args.length + 2];
			allArgs[allArgs.length - 2] = "-b";
			allArgs[allArgs.length - 1] = TestUtils.getTestUrl();
			for(int i = 0; i < args.length; ++i)
				allArgs[i] = args[i];
			args = allArgs;
		}
		org.schedulesdirect.grabber.Grabber.main(args);
	}
	
	private void resetLineups(String type) throws Exception {
		NetworkEpgClient clnt = TestUtils.getClientFor(type);
		try {
			for(Headend he : clnt.getHeadends())
				clnt.removeHeadendFromAccount(he.getId());
			assertTrue(clnt.getHeadends().length == 0);
			for(String he : new String[] {"0005995", "PC:K7P3G6"})
				clnt.addHeadendToAccount(he);
			assertTrue(clnt.getHeadends().length == 2);
		} finally {
			if(clnt != null) clnt.close();
		}
	}
}